﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Data.OleDb;
using System.Threading;
using System.ComponentModel;
using System.Data;
using System.Net;
using Visifire.Charts;
using Visifire.Commons;
using System.Collections.ObjectModel;
using EnerForecast;
using System.Text.RegularExpressions;



namespace EnerForecast
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        List<string> sheets;
        Chart costphourlinechart;
        ChartInformation ciLineChart;
        double[] arr_result,arr_result_heat;
        double[] arr_postCostHeat;
        double[] arr_postCost; double[] arr_postCost2; double[] arr_postCost3; double[] arr_postCost4;
        double[] arr_postCost5; double[] arr_postCost6; double[] arr_postCost7;
        double[] arr_smoothedResult,arr_smoothedResult_heat;
        double[] arr_realsmoothedResult;
        double[] week_elect = new double[168];
        double[] week_water = new double[168];
        double[] week_postCost = new double[168];
        string[] week_time = new string[168];
        double realelec = 0;
        double postelec = 0;
        double safe = 5000;
        DataSeries dsComputeResult, dsPostCost, dsSmooth,dsComputeResultHeat,dsPostCostHeat,dsSmoothHeat;
        ChartInformation ciEconomic1;
        DataSeries ds1;
        DataSeries ds2;
        Random random = new Random();
        //插入温度日总能耗信息

        //计算耗能
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            label8.Visibility = Visibility.Hidden;
            // label9.Visibility = Visibility.Hidden;
            label10.Visibility = Visibility.Hidden;
            btn_CalculateDist.IsEnabled = true;
            CostPHourList.IsSelected = true;
            OptDistResult.IsEnabled = false;
            NorDistResult.IsEnabled = false;
            OptDistResultweek.IsEnabled = false;
            OptDistResult1.IsEnabled = false;
            CostPHoreChart2.IsEnabled = false;
            double mintemp = 0;
            double maxtemp = 0;
            double wet = 0;
            ForeCastResult result = null; ForeCastResult result2 = null; ForeCastResult result3 = null; ForeCastResult result4 = null; ForeCastResult result5 = null;
            ForeCastResult result6 = null; ForeCastResult result7 = null;
            DateTime temp;
            try
            {
                mintemp = double.Parse(minTemp.Text.ToString());
                maxtemp = double.Parse(maxTemp.Text.ToString());
                wet = double.Parse(Wet.Text.ToString());
                //转为湿球温度
                double yongdian = double.Parse(textBox1.Text.ToString());
                double DryTemp = Math.Floor((maxtemp + mintemp) / 2);
                Dry2Wet Tempconvert = new Dry2Wet();
                double WetTemp = Tempconvert.Convert2wet(int.Parse(DryTemp.ToString()), wet);
                //mintemp = (wet - 40.6) / 0.72 - mintemp;
                //maxtemp = (wet - 40.6) / 0.72 - maxtemp;
                temp = (DateTime)calendar1.SelectedDate;
                result = new ForeCastResult(temp, WetTemp, maxtemp);
                //7天实际值
                result2 = new ForeCastResult(temp.AddDays(1), WetTemp, maxtemp);
                result3 = new ForeCastResult(temp.AddDays(2), WetTemp, maxtemp);
                result4 = new ForeCastResult(temp.AddDays(3), WetTemp, maxtemp);
                result5 = new ForeCastResult(temp.AddDays(4), WetTemp, maxtemp);
                result6 = new ForeCastResult(temp.AddDays(5), WetTemp, maxtemp);
                result7 = new ForeCastResult(temp.AddDays(6), WetTemp, maxtemp);
                //7天实际值

            }
            catch (Exception)
            {
                MessageBox.Show("请确认输入字串正确或已经选择日期");
                return;
            }
            //////////////////////////////////////
            if (maxtemp - mintemp > 30 || maxtemp - mintemp < 0)
            {
                MessageBox.Show("请确气温数据正常");
                return;
            }
            if (!result.checkCoe())
            {
                MessageBox.Show("请确认历史数据已导入");
                return;
            }
            arr_result = result.getComputeResult();  //冷负荷初步预测值
            arr_result_heat = result.getComputeResultHeat();//热负荷初步预测值

            //7天实际值+某天热负荷
            arr_postCostHeat = result.getPostCost1();
            arr_postCost = result.getPostCost();
            arr_postCost2 = result2.getPostCost();
            arr_postCost3 = result3.getPostCost();
            arr_postCost4 = result4.getPostCost();
            arr_postCost5 = result5.getPostCost();
            arr_postCost6 = result6.getPostCost();
            arr_postCost7 = result7.getPostCost();
            //7天实际值
            arr_smoothedResult = result.getSmoothedResult(arr_result, arr_postCost);
            arr_smoothedResult_heat = result.getSmoothedResult(arr_result_heat, arr_postCostHeat);
            double totalcost = 0, totalPostcost = 0, totalSmoothed = 0;
            //表信息设置
            ObservableCollection<ClockCost> resultData = new ObservableCollection<ClockCost>();
            for (int i = 0; i < 24; i++)
            {
                resultData.Add(new ClockCost()
                {
                    Clock = (i + 1).ToString(),
                    Cost = arr_result[i].ToString("0.00"),
                    PostCost = arr_postCost[i].ToString("0.00"),
                    SmoothedResult = arr_smoothedResult[i].ToString("0.00")
                });
                totalcost += arr_result[i];
                totalPostcost += arr_postCost[i];
                totalSmoothed += arr_smoothedResult[i];
            }
            //显示总能耗
            resultData.Add(new ClockCost()
            {
                Clock = "当日总能耗",
                Cost = totalcost.ToString("0.00"),
                PostCost = totalPostcost.ToString("0.00"),
                SmoothedResult = totalSmoothed.ToString("0.00")
            });
            datagrid.DataContext = resultData;
            //图信息设置
            ciLineChart = new ChartInformation()
            {
                title = temp.Month.ToString() + "月" + temp.Day.ToString() + "日逐时负荷预测结果",
                m_axisXTitle = "时刻",
                m_axisYTitle = "负荷（KW）",
                dsc = new DataSeriesCollection()
            };
            //图数据
            DataPointCollection dpcForeCast = new DataPointCollection();
            for (int i = 0; i < 24; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                    AxisXLabel = (i + 1).ToString(),
                    YValue = arr_result[i]
                };

                dpcForeCast.Add(dptemp);
            }
            dsComputeResult = new DataSeries()
            {
                LegendText = "初步预测值",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,
                DataPoints = new DataPointCollection(dpcForeCast),
            };
            dsComputeResult.Opacity = 0;

            //图数据2
            DataPointCollection dpcPostCost = new DataPointCollection();
            for (int i = 0; i < 24; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                    AxisXLabel = (i + 1).ToString(),
                    YValue = arr_postCost[i]
                };

                dpcPostCost.Add(dptemp);
            }
            dsPostCost = new DataSeries()
            {
                LegendText = "模拟实际值",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,

                DataPoints = new DataPointCollection(dpcPostCost),
            };

            //图数据3
            DataPointCollection dpcSmoothedResult = new DataPointCollection();
            for (int i = 0; i < 24; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                    AxisXLabel = (i + 1).ToString(),
                    YValue = arr_smoothedResult[i],
                };

                dpcSmoothedResult.Add(dptemp);
            }
            dsSmooth = new DataSeries()
            {
                LegendText = "指数平滑后结果",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,
                DataPoints = new DataPointCollection(dpcSmoothedResult)
            };

            //图数据4
            DataPointCollection dpcForeCastHeat = new DataPointCollection();
            for (int i = 0; i < 24; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                    AxisXLabel = (i + 1).ToString(),
                    YValue = arr_result_heat[i],
                };

                dpcForeCastHeat.Add(dptemp);
            }
            dsComputeResultHeat = new DataSeries()
            {
                LegendText = "热负荷初步预测值",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,
                DataPoints = new DataPointCollection(dpcForeCastHeat)
            };

            //图数据5
            DataPointCollection dpcPostCostHeat = new DataPointCollection();
            for (int i = 0; i < 24; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                    AxisXLabel = (i + 1).ToString(),
                    YValue = arr_postCostHeat[i],
                };

                dpcPostCostHeat.Add(dptemp);
            }
            dsPostCostHeat = new DataSeries()
            {
                LegendText = "热负荷实际",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,
                DataPoints = new DataPointCollection(dpcPostCostHeat)
            };

            //图数据6
            DataPointCollection dpcSmoothedResultHeat = new DataPointCollection();
            for (int i = 0; i < 24; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                    AxisXLabel = (i + 1).ToString(),
                    YValue = arr_smoothedResult_heat[i],
                };

                dpcSmoothedResultHeat.Add(dptemp);
            }
            dsSmoothHeat = new DataSeries()
            {
                LegendText = "热负荷预测值",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,
                DataPoints = new DataPointCollection(dpcSmoothedResultHeat)
            };

            ChartHelper ch = new ChartHelper();
            ciLineChart.dsc.Clear();
           // ciLineChart.dsc.Add(dsComputeResult);
          //  ciLineChart.dsc.Add(dsPostCost);
            ciLineChart.dsc.Add(dsSmooth);
           ciLineChart.dsc.Add(dsComputeResultHeat);
         ciLineChart.dsc.Add(dsPostCostHeat);
           ciLineChart.dsc.Add(dsSmoothHeat);


            costphourlinechart = ch.CreatChart(ciLineChart);
            Layout.Children.Clear();
            Layout.Children.Add(costphourlinechart);





            double pinghuawucha1 = 0; double pinghuawuchazong = 0; string pinghuawucha = "";
            for (int i = 0; i < 24; i++)
            {

                pinghuawucha1 = pinghuawucha1 + System.Math.Abs(arr_smoothedResult[i] - arr_postCost[i]);
                pinghuawuchazong = pinghuawuchazong + arr_postCost[i];
                pinghuawucha = (0.5 * pinghuawucha1 / pinghuawuchazong * 100).ToString("f2") + "%";
                label9.Content = "预测误差：" + pinghuawucha;
            }
        }

        public class ClockCost
        {
            public string Clock { get; set; }
            public string Cost { get; set; }
            public string PostCost { get; set; }
            public string SmoothedResult { get; set; }
        }


        private void btn_CalculateDist_Click(object sender, RoutedEventArgs e)
        {
            btn_CalculateDist.IsEnabled = false;
            if (arr_smoothedResult.Length != 24)
            {
                MessageBox.Show("值异常!");
                return;
            }
            OptDistResult.IsEnabled = true;
            NorDistResult.IsEnabled = true;
            OptDistResult1.IsEnabled = true;
            OptDistResultweek.IsEnabled = true;
            CostPHoreChart2.IsEnabled = true;
            //OptDistResult.IsSelected = true;
            NorDistResult.IsSelected = true;
            Distributer dis = new Distributer(arr_smoothedResult);
            //初步  DataTable dtNor = dis.Calculate(false);
            // DataTable dtOPT = dis.Calculate(true);
            DataTable dtOPT = Calculate(true);
            Distributer dis1 = new Distributer(arr_realsmoothedResult);
            DataTable dtOPT1 = Calculate(false);
            DataTable dtNor = CommonCalculate();
            DataTable dtOPTweek = Calculateweek();
            OptlvResultweek.ItemsSource = dtOPTweek.DefaultView;
            NorlvResult.ItemsSource = dtNor.DefaultView;
            OptlvResult.ItemsSource = dtOPT.DefaultView;
            OptlvResult1.ItemsSource = dtOPT1.DefaultView;
            label10.Visibility = Visibility.Visible;
            label10.Content = "预测节约电制冷制冷量：" + (realelec - postelec).ToString("0.00") + "KW";
            label8.Visibility = Visibility.Visible;
            label8.Content = "节约电量（估计）:" + ((realelec - postelec) / 5).ToString("0.00") + "KWH";



            //图信息设置
            ciLineChart = new ChartInformation()
            {
                title = "7日逐时负荷预测结果",
                m_axisXTitle = "时刻",
                m_axisYTitle = "负荷（KW）",
                dsc = new DataSeriesCollection()
            };
            //图数据
            DataPointCollection dpcForeCast = new DataPointCollection();
            for (int i = 0; i < 168; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                   // AxisXLabel = (i + 1).ToString(),
                    AxisXLabel = week_time[i],
                    YValue = week_elect[i]
                };

                dpcForeCast.Add(dptemp);
            }
            dsComputeResult = new DataSeries()
            {
                LegendText = "电制冷量",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,
                DataPoints = new DataPointCollection(dpcForeCast),
            };
            dsComputeResult.Opacity = 0;

            //图数据2
            DataPointCollection dpcPostCost = new DataPointCollection();
            for (int i = 0; i < 168; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                   // AxisXLabel = (i + 1).ToString(),
                    AxisXLabel = week_time[i],
                    YValue = week_water[i]
                };

                dpcPostCost.Add(dptemp);
            }
            dsPostCost = new DataSeries()
            {
                LegendText = "水蓄冷量",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,

                DataPoints = new DataPointCollection(dpcPostCost),
            };

            //图数据3
            DataPointCollection dpcSmoothedResult = new DataPointCollection();
            for (int i = 0; i < 168; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                   // AxisXLabel = (i + 1).ToString(),
                   AxisXLabel = week_time[i],
                    YValue = week_postCost[i],
                };

                dpcSmoothedResult.Add(dptemp);
            }
            dsSmooth = new DataSeries()
            {
                LegendText = "负荷值",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,
                DataPoints = new DataPointCollection(dpcSmoothedResult)
            };

            ChartHelper ch = new ChartHelper();
            ciLineChart.dsc.Clear();
            ciLineChart.dsc.Add(dsComputeResult);
            ciLineChart.dsc.Add(dsPostCost);
            ciLineChart.dsc.Add(dsSmooth);



            costphourlinechart = ch.CreatChart(ciLineChart);
            Layout2.Children.Clear();
            Layout2.Children.Add(costphourlinechart);



        }

        private void btn_LoadSample_Click(object sender, RoutedEventArgs e)
        {
            label8.Visibility = Visibility.Hidden;
            //label9.Visibility = Visibility.Hidden;
            label10.Visibility = Visibility.Hidden;
            btn_CalculateDist.IsEnabled = true;
            NorDistResult.IsEnabled = false;
            OptDistResult.IsEnabled = false;
            OptDistResult1.IsEnabled = false;
            OptDistResultweek.IsEnabled = false;
            CostPHoreChart2.IsEnabled = false;
            //实际  OptDistResult1.IsEnabled = false;

            CostPHoreChart.IsSelected = true;
            arr_realsmoothedResult = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            arr_smoothedResult = new double[]{0,0,0,0,0,0,           
            4598,  //六点开始冷量需求
            9808, 
            16858, //八点高电价
            27279, //九点高电价
            27892, //十点高电价
            26359 ,
            26359 ,
            27279 ,//十三点高电价
            30650 ,//十四点高电价
            30650 ,
            27585 ,
            17471 ,
            7663 ,//十八点高电价
            6743 ,//十九点高电价
            5517 ,//二十点高电价
            4598 ,
            0,0};//二十二点之后停止冷量需求
            //实际值
            double[] iresult = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            for (int i = 0; i < 24; i++)
                iresult[i] = random.NextDouble();

            for (int i = 0; i < 24; i++)
            {
                arr_realsmoothedResult[i] = arr_smoothedResult[i] * (1 + 0.3 * (iresult[i] - 0.5));
            }



            double rate = 1;
            try
            {
                rate = int.Parse(tb_Rate.Text) * 0.01;
            }
            catch (Exception)
            {
                MessageBox.Show("实验数据比例值有误");
            }

            for (int i = 0; i < arr_smoothedResult.Length; i++)
            {
                arr_smoothedResult[i] = rate * arr_smoothedResult[i];
                arr_realsmoothedResult[i] = rate * arr_realsmoothedResult[i];
            }
            ciLineChart = new ChartInformation()
           {
               title = "设计负荷",
               m_axisXTitle = "时刻",
               m_axisYTitle = "负荷（KW）",
               dsc = new DataSeriesCollection()
           };
            DataPointCollection dpcSample = new DataPointCollection();
            for (int i = 0; i < 24; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                    AxisXLabel = (i + 1).ToString(),
                    YValue = arr_smoothedResult[i],
                };

                dpcSample.Add(dptemp);
            }
            DataSeries dsSample = new DataSeries()
            {
                LegendText = "设计实验数据",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,
                DataPoints = new DataPointCollection(dpcSample)
            };


            DataPointCollection dpsSample1 = new DataPointCollection();
            for (int i = 0; i < 24; i++)
            {
                DataPoint dptemp = new DataPoint()
                {
                    AxisXLabel = (i + 1).ToString(),
                    YValue = arr_realsmoothedResult[i]
                };

                dpsSample1.Add(dptemp);
            }

            DataSeries dsSample1 = new DataSeries()
            {
                LegendText = "实际实验数据",
                RenderAs = RenderAs.Line,
                AxisYType = AxisTypes.Primary,

                DataPoints = new DataPointCollection(dpsSample1),
            };


            ChartHelper ch = new ChartHelper();
            ciLineChart.dsc.Clear();
            ciLineChart.dsc.Add(dsSample);
            ciLineChart.dsc.Add(dsSample1);



            costphourlinechart = ch.CreatChart(ciLineChart);
            Layout.Children.Clear();
            Layout.Children.Add(costphourlinechart);
        }


        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void btn_Load_Click(object sender, RoutedEventArgs e)
        {
            Window load = new LoadDataWindow();
            load.Show();
        }




        public DataTable Calculate(bool a)
        {
            double[] DLOADS = new double[24];
            double[] curLoad = new double[24];//[5]=[总负荷][冰蓄冷供应][电制冷供应][常驻能源供应][双工况]      //需求负荷
            double[] deicingLoad = new double[24];//[5]=[总负荷][冰蓄冷供应][电制冷供应][常驻能源供应][双工况]   //水蓄冷用量
            double[] shuicun = new double[24]; //水寸量
            double[] electCoolingLoad = new double[24];//[5]=[总负荷][冰蓄冷供应][电制冷供应][常驻能源供应][双工况]  //电制冷
            double[] xiuhuali = new double[24]{
            19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655};//[制冰负荷]
            double temp = Convert.ToDouble(textBox1.Text);
            double[] upload = new double[24]{
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
            // double safe=5000;
            double safe2 = safe;

            //  double[] inputLoads;
            // Array.Copy(inputLoads, DLOADS, 24);
            //当前负荷
            for (int i = 0; i < 24; i++)
            {
                curLoad[i] = arr_smoothedResult[i];
                if (curLoad[i] - 39780 - 19655 > 0)
                    safe += curLoad[i] - 39780 - 19655;
            }
            if (a)
            {
                for (int i = 0; i < 24; i++)
                {
                    curLoad[i] = arr_smoothedResult[i];
                   
                }
            }
            else
            {
                for (int i = 0; i < 24; i++)
                {
                    curLoad[i] = arr_postCost[i];
                }
            }
            //yuce

            #region//满足峰时用电
            for (int i = 0; i < 24; i++)
            {
                if (xiuhuali[i] >= curLoad[i])       //负荷小于溴化锂
                {
                    if (temp + xiuhuali[i] - curLoad[i] < 128000)    //蓄水池未满
                    {
                        temp += xiuhuali[i] - curLoad[i];
                        shuicun[i] = temp;
                        deicingLoad[i] = 0;
                        electCoolingLoad[i] = 0;
                    }
                    else         //蓄水池满
                    {
                        xiuhuali[i] = 128000 - temp + curLoad[i];
                        temp = 128000;
                        shuicun[i] = temp;
                        deicingLoad[i] = 0;
                        electCoolingLoad[i] = 0;
                    }
                }
                else          //溴化锂不够负荷
                {
                    if (temp + xiuhuali[i] - curLoad[i] >= safe)         //只用水和溴化锂
                    {
                        temp += xiuhuali[i] - curLoad[i];
                        shuicun[i] = temp;
                        deicingLoad[i] = curLoad[i] - xiuhuali[i];
                        electCoolingLoad[i] = 0;
                    }
                    else                                           //全开
                    {
                        if ((curLoad[i] - 39780 - 19655) < 0)             //负荷小于电+溴化锂
                        {
                            if (temp - safe > 0)                                //蓄水池余量大于安全量
                            {
                                deicingLoad[i] = temp - safe;
                                temp = safe;
                                xiuhuali[i] = 19655;
                                shuicun[i] = temp;
                                electCoolingLoad[i] = curLoad[i] - xiuhuali[i] - deicingLoad[i];
                            }
                            else                                         //蓄水池余量不够安全量
                            {
                                deicingLoad[i] = 0;
                                xiuhuali[i] = 19655;
                                shuicun[i] = temp;
                                electCoolingLoad[i] = curLoad[i] - xiuhuali[i];
                            }
                        }
                        else                                              //负荷大于电+溴化锂
                        {
                            if (temp - safe <= 0)                        //蓄水池余量不够安全量
                            {
                                electCoolingLoad[i] = 39780;
                                xiuhuali[i] = 19655;
                                deicingLoad[i] = curLoad[i] - electCoolingLoad[i] - xiuhuali[i];
                                temp -= deicingLoad[i];
                                shuicun[i] = temp;
                            }
                            else                                            //蓄水池余量大于安全量
                            {
                                xiuhuali[i] = 19655;
                                deicingLoad[i] = temp - safe + curLoad[i] - 39780 - 19655;
                                temp -= deicingLoad[i];
                                electCoolingLoad[i] = curLoad[i] - xiuhuali[i] - deicingLoad[i];
                                shuicun[i] = temp;
                            }
                        }

                    }
                }

            }
            #endregion
     

            DataTable ResultTable = new DataTable();
            ResultTable.Columns.Add("时间节点", typeof(string));
            ResultTable.Columns.Add("需求负荷", typeof(string));
            ResultTable.Columns.Add("水蓄冷用量", typeof(string));
            ResultTable.Columns.Add("蓄水池存量", typeof(string));
            ResultTable.Columns.Add("电制冷", typeof(string));
            ResultTable.Columns.Add("溴化锂", typeof(string));

            double[] tempCost = new double[24];
            double[] totalCost = new double[24];
            double[] tempsum = { 0, 0, 0, 0, 0, 0, 0, 0 };//需求总负荷 溶冰提供总负荷 电制冷提供总符合 
            //电制冷总能耗 空调提供总负荷，空调总能耗，系统总能耗，系统总电价
            for (int i = 0; i < 24; i++)
            {

                ResultTable.Rows.Add(new object[] { i,                    
                   curLoad[i].ToString("0.0"),
                   deicingLoad[i].ToString("0.0"),
                   shuicun[i].ToString("0.0"),
                   electCoolingLoad[i].ToString("0.0"),//电制冷量
                   xiuhuali[i].ToString("0.0")
                });
                //加总
                tempsum[0] += curLoad[i];
                tempsum[1] += deicingLoad[i];
                tempsum[2] += electCoolingLoad[i];
                tempsum[3] += xiuhuali[i];


            }
            if (a)
                postelec = tempsum[2];
            tempsum[4] = shuicun[23];
            ResultTable.Rows.Add(new object[] { "总计", tempsum[0].ToString("0.0"), tempsum[1].ToString("0.0"), tempsum[4].ToString("0.0"), tempsum[2].ToString("0.0"), tempsum[3].ToString("0.0") });
            //ResultTable.Rows.Add(new object[] { "总计", "冰蓄冷融冰结束时间：" + CutTime.ToString() + " :" + CutMinute.ToString(), tempsum[0].ToString("0.0"), tempsum[1].ToString("0.0"), tempsum[2].ToString("0.0"), tempsum[3].ToString("0.0"), "31856.0", tempsum[4].ToString("0.0"), tempsum[5].ToString("0.0"), "94536.0", "23848.0", tempsum[6].ToString("0.0"), tempsum[7].ToString("0.000") });
            safe = safe2;
            return ResultTable;

        }

        public DataTable CommonCalculate()
        {
            double[] DLOADS = new double[24];
            double[] curLoad = new double[24];//[5]=[总负荷][冰蓄冷供应][电制冷供应][常驻能源供应][双工况]      //需求负荷
            double[] deicingLoad = new double[24];//[5]=[总负荷][冰蓄冷供应][电制冷供应][常驻能源供应][双工况]   //水蓄冷用量
            double[] shuicun = new double[24]; //水寸量
            double[] electCoolingLoad = new double[24];//[5]=[总负荷][冰蓄冷供应][电制冷供应][常驻能源供应][双工况]  //电制冷
            double[] xiuhuali = new double[24]{
            19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655};//[制冰负荷]
            double temp = Convert.ToDouble(textBox1.Text);
            double[] upload = new double[24]{
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
            //  safe =Convert.ToDouble(safetxt.ToString());

            //  double[] inputLoads;
            // Array.Copy(inputLoads, DLOADS, 24);
            //当前负荷
            for (int i = 0; i < 24; i++)
            {
                curLoad[i] = arr_postCost[i];

            }

            //yuce

            #region//满足峰时用电
            for (int i = 0; i < 24; i++)
            {
                if (xiuhuali[i] >= curLoad[i])       //负荷小于溴化锂
                {
                    if (temp + xiuhuali[i] - curLoad[i] < 128000)    //蓄水池未满
                    {
                        temp += xiuhuali[i] - curLoad[i];
                        shuicun[i] = temp;
                        deicingLoad[i] = 0;
                        electCoolingLoad[i] = 0;
                    }
                    else         //蓄水池满
                    {
                        xiuhuali[i] = 128000 - temp + curLoad[i];
                        temp = 128000;
                        shuicun[i] = temp;
                        deicingLoad[i] = 0;
                        electCoolingLoad[i] = 0;
                    }
                }
                else          //溴化锂不够负荷
                {
                    xiuhuali[i] = 19655;
                    if (curLoad[i] <= 19655 + 39780)
                    {
                        shuicun[i] = temp;
                        deicingLoad[i] = 0;
                        electCoolingLoad[i] = curLoad[i] - 19655;

                    }
                    else
                    {
                        electCoolingLoad[i] = 39780;
                        deicingLoad[i] = curLoad[i] - 19655 - 39780;
                        temp -= deicingLoad[i];
                        shuicun[i] = temp;
                    }



                }

            }
            #endregion
            #region//根据得出的冷量计算每小时制冷方式的能耗


            #endregion

            DataTable ResultTable = new DataTable();
            ResultTable.Columns.Add("时间节点", typeof(string));
            ResultTable.Columns.Add("需求负荷", typeof(string));
            ResultTable.Columns.Add("水蓄冷用量", typeof(string));
            ResultTable.Columns.Add("蓄水池存量", typeof(string));
            ResultTable.Columns.Add("电制冷", typeof(string));
            ResultTable.Columns.Add("溴化锂", typeof(string));

            double[] tempCost = new double[24];
            double[] totalCost = new double[24];
            double[] tempsum = { 0, 0, 0, 0, 0, 0, 0, 0 };//需求总负荷 溶冰提供总负荷 电制冷提供总符合 
            //电制冷总能耗 空调提供总负荷，空调总能耗，系统总能耗，系统总电价
            for (int i = 0; i < 24; i++)
            {

                ResultTable.Rows.Add(new object[] { i,                    
                   curLoad[i].ToString("0.0"),
                   deicingLoad[i].ToString("0.0"),
                   shuicun[i].ToString("0.0"),
                   electCoolingLoad[i].ToString("0.0"),//电制冷量
                   xiuhuali[i].ToString("0.0")
                });
                //加总
                tempsum[0] += curLoad[i];
                tempsum[1] += deicingLoad[i];
                tempsum[2] += electCoolingLoad[i];
                tempsum[3] += xiuhuali[i];


            }
            realelec = tempsum[2];
            tempsum[4] = shuicun[23];
            ResultTable.Rows.Add(new object[] { "总计", tempsum[0].ToString("0.0"), tempsum[1].ToString("0.0"), tempsum[4].ToString("0.0"), tempsum[2].ToString("0.0"), tempsum[3].ToString("0.0") });
            //ResultTable.Rows.Add(new object[] { "总计", "冰蓄冷融冰结束时间：" + CutTime.ToString() + " :" + CutMinute.ToString(), tempsum[0].ToString("0.0"), tempsum[1].ToString("0.0"), tempsum[2].ToString("0.0"), tempsum[3].ToString("0.0"), "31856.0", tempsum[4].ToString("0.0"), tempsum[5].ToString("0.0"), "94536.0", "23848.0", tempsum[6].ToString("0.0"), tempsum[7].ToString("0.000") });
            return ResultTable;

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            maxTemp.Text = "35"; minTemp.Text = "28"; Wet.Text = "80"; textBox1.Text = "5000";
            calendar1.SelectedDate = new DateTime(2013, 8, 14);
            calendar1.DisplayDate = new DateTime(2013, 8, 14);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            button2.IsEnabled = false;
            safetxt.Visibility = Visibility.Visible;
            label11.Visibility = Visibility.Visible;
            button3.Visibility = Visibility.Visible;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (IsNumberic(safetxt.Text))
            {
                button2.IsEnabled = true;
                safetxt.Visibility = Visibility.Hidden;
                label11.Visibility = Visibility.Hidden;
                button3.Visibility = Visibility.Hidden;
                safe = Convert.ToDouble(safetxt.Text);
            }
            else
                MessageBox.Show("请输入大于0的正整数！");
        }


        public bool IsNumberic(string str)
        {

            Regex reg = new Regex("^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$");
            Match ma = reg.Match(str);
            if (ma.Success)
            {
                //是数字  
                return true;
            }
            else
            {
                //不是数字    
                return false;
            }
        }


        public DataTable Calculateweek()
        {
            double[] DLOADS = new double[24];
            double[] curLoad = new double[24];//[5]=[总负荷][冰蓄冷供应][电制冷供应][常驻能源供应][双工况]      //需求负荷
            double[] deicingLoad = new double[24];//[5]=[总负荷][冰蓄冷供应][电制冷供应][常驻能源供应][双工况]   //水蓄冷用量
            double[] shuicun = new double[24]; //水寸量
            double[] electCoolingLoad = new double[24];//[5]=[总负荷][冰蓄冷供应][电制冷供应][常驻能源供应][双工况]  //电制冷
            double[] xiuhuali = new double[24]{
            19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655,19655};//[制冰负荷]
            double temp = Convert.ToDouble(textBox1.Text);
            double[] upload = new double[24]{
                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
            double safe1 = safe;
            // double safe=5000;

            //  double[] inputLoads;
            // Array.Copy(inputLoads, DLOADS, 24);
            //当前负荷

            DataTable ResultTable = new DataTable();
            ResultTable.Columns.Add("时间节点", typeof(string));
            ResultTable.Columns.Add("需求负荷", typeof(string));
            ResultTable.Columns.Add("水蓄冷用量", typeof(string));
            ResultTable.Columns.Add("蓄水池存量", typeof(string));
            ResultTable.Columns.Add("电制冷", typeof(string));
            ResultTable.Columns.Add("溴化锂", typeof(string));

            for (int j = 1; j < 8; j++)
            {



                for (int i = 0; i < 24; i++)
                {
                    if (j == 1)
                        curLoad[i] = arr_postCost[i];
                    else if(j==2)
                        curLoad[i] = arr_postCost2[i];
                    else if (j == 3)
                        curLoad[i] = arr_postCost3[i];
                    else if (j == 4)
                        curLoad[i] = arr_postCost4[i];
                    else if (j == 5)
                        curLoad[i] = arr_postCost5[i];
                    else if (j == 6)
                        curLoad[i] = arr_postCost6[i];
                    else if (j == 7)
                        curLoad[i] = arr_postCost7[i];
                    if (curLoad[i] - 39780 - 19655 > 0)
                        safe += curLoad[i] - 39780 - 19655;
                }

                //yuce

                #region//满足峰时用电
                for (int i = 0; i < 24; i++)
                {
                    if (xiuhuali[i] >= curLoad[i])       //负荷小于溴化锂
                    {
                        if (temp + xiuhuali[i] - curLoad[i] < 128000)    //蓄水池未满
                        {
                            temp += xiuhuali[i] - curLoad[i];
                            shuicun[i] = temp;
                            deicingLoad[i] = 0;
                            electCoolingLoad[i] = 0;
                        }
                        else         //蓄水池满
                        {
                            xiuhuali[i] = 128000 - temp + curLoad[i];
                            temp = 128000;
                            shuicun[i] = temp;
                            deicingLoad[i] = 0;
                            electCoolingLoad[i] = 0;
                        }
                    }
                    else          //溴化锂不够负荷
                    {
                        if (temp + xiuhuali[i] - curLoad[i] >= safe)         //只用水和溴化锂
                        {
                            temp += xiuhuali[i] - curLoad[i];
                            shuicun[i] = temp;
                            deicingLoad[i] = curLoad[i] - xiuhuali[i];
                            electCoolingLoad[i] = 0;
                        }
                        else                                           //全开
                        {
                            if ((curLoad[i] - 39780 - 19655) < 0)             //负荷小于电+溴化锂
                            {
                                if (temp - safe > 0)                                //蓄水池余量大于安全量
                                {
                                    deicingLoad[i] = temp - safe;
                                    temp = safe;
                                    xiuhuali[i] = 19655;
                                    shuicun[i] = temp;
                                    electCoolingLoad[i] = curLoad[i] - xiuhuali[i] - deicingLoad[i];
                                }
                                else                                         //蓄水池余量不够安全量
                                {
                                    deicingLoad[i] = 0;
                                    xiuhuali[i] = 19655;
                                    shuicun[i] = temp;
                                    electCoolingLoad[i] = curLoad[i] - xiuhuali[i];
                                }
                            }
                            else                                              //负荷大于电+溴化锂
                            {
                                if (temp - safe <= 0)                        //蓄水池余量不够安全量
                                {
                                    electCoolingLoad[i] = 39780;
                                    xiuhuali[i] = 19655;
                                    deicingLoad[i] = curLoad[i] - electCoolingLoad[i] - xiuhuali[i];
                                    temp -= deicingLoad[i];
                                    shuicun[i] = temp;
                                }
                                else                                            //蓄水池余量大于安全量
                                {
                                    xiuhuali[i] = 19655;
                                    deicingLoad[i] = temp - safe + curLoad[i] - 39780 - 19655;
                                    temp -= deicingLoad[i];
                                    electCoolingLoad[i] = curLoad[i] - xiuhuali[i] - deicingLoad[i];
                                    shuicun[i] = temp;
                                }
                            }

                        }
                    }
                    week_elect[(j - 1) * 24 + i] = electCoolingLoad[i];
                    week_postCost[(j - 1) * 24 + i] = curLoad[i];
                    week_water[(j - 1) * 24 + i] = deicingLoad[i];
                    week_time[(j - 1) * 24 + i]=calendar1.SelectedDate.Value.AddDays(j).Month+"/"+calendar1.SelectedDate.Value.AddDays(j).Day+" "+i+"时";
                }
                #endregion

                double[] tempCost = new double[24];
                double[] totalCost = new double[24];
                double[] tempsum = { 0, 0, 0, 0, 0, 0, 0, 0 };//需求总负荷 溶冰提供总负荷 电制冷提供总符合 
                //电制冷总能耗 空调提供总负荷，空调总能耗，系统总能耗，系统总电价
                for (int i = 0; i < 24; i++)
                {

                    ResultTable.Rows.Add(new object[] { i,                    
                   curLoad[i].ToString("0.0"),
                   deicingLoad[i].ToString("0.0"),
                   shuicun[i].ToString("0.0"),
                   electCoolingLoad[i].ToString("0.0"),//电制冷量
                   xiuhuali[i].ToString("0.0")
                });
                    //加总
                    tempsum[0] += curLoad[i];
                    tempsum[1] += deicingLoad[i];
                    tempsum[2] += electCoolingLoad[i];
                    tempsum[3] += xiuhuali[i];


                }
                tempsum[4] = shuicun[23];
                
                ResultTable.Rows.Add(new object[] { "总计" + calendar1.SelectedDate.Value.AddDays(j-1).ToShortDateString(), tempsum[0].ToString("0.0"), tempsum[1].ToString("0.0"), tempsum[4].ToString("0.0"), tempsum[2].ToString("0.0"), tempsum[3].ToString("0.0") });
                //ResultTable.Rows.Add(new object[] { "总计", "冰蓄冷融冰结束时间：" + CutTime.ToString() + " :" + CutMinute.ToString(), tempsum[0].ToString("0.0"), tempsum[1].ToString("0.0"), tempsum[2].ToString("0.0"), tempsum[3].ToString("0.0"), "31856.0", tempsum[4].ToString("0.0"), tempsum[5].ToString("0.0"), "94536.0", "23848.0", tempsum[6].ToString("0.0"), tempsum[7].ToString("0.000") });
                safe = safe1;
            }
            return ResultTable;

        }
    }


    
}
